home *** CD-ROM | disk | FTP | other *** search
/ Especial Multimedia / Especial Multimedia.iso / Multimed / Prg / STR2BMP.ZIP / STR2BMP.CPP < prev    next >
C/C++ Source or Header  |  1997-09-14  |  16KB  |  583 lines

  1. // Strbmp.cpp : Code for VB string to bitmap conversion DLL.
  2. //
  3. //   Freeware -- Version 1.1   09/20/93
  4. //
  5. //   Written by Jorge Monasterio (CIS 72147, 2674)
  6. //
  7. //
  8.  
  9. /* Header files. */
  10. #include <windows.h>
  11. #include <string.h>
  12. #include "c:\vb\cdk\vbapi.h"
  13. #include "str2bmp.h"
  14. #pragma hdrstop
  15.                 
  16. // DESCRIPTION: DLL Initialization code.
  17. // PARAMETERS: Windows stuff.
  18. // RETURN VALUE: 1 if succesfully initialization, else 0.
  19. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  20.  { return 1;
  21.  }
  22.  
  23. // DESCRIPTION: DLL removal code.
  24. // PARAMETERS: Windows stuff.
  25. // RETURN VALUE: 1 if succesfully initialization, else 0.
  26. int CALLBACK WEP (int nParameter)
  27.  { // Dll removal successful.
  28.    return 1;
  29.  }
  30.  
  31. // DESCRIPTION: Display a runtime error message to VB and return error code.
  32. // RETURN VALUE: **** THIS FUNCTION DOES NOT RETURN ****
  33. DLL(void) ReturnVBError( ERR err, LPSTR msg)
  34.  { VBRuntimeError( VBSetErrorMessage( err, msg));
  35.  }
  36.  
  37. //    DESCRIPTION:
  38. //        Function for converting VB bitmap to string.
  39. //    PARAMETERS:
  40. //        hdc : Picture.hdc
  41. //           himage : Picture.image
  42. //           string : VB string. Must be large enough to hold data. See
  43. //                         Bmp_GetSize for more info.
  44. //    RETURN VALUE:
  45. //        VB string containing the bitmap information.
  46. //    SYNTAX IN VB:
  47. //        A$ = BmpToString( Picture1.hdc, Picture1.image)
  48. DLL(HLSTR)  BMPToString( HDC hdc, HBITMAP hImage)
  49.  { // Local variables.
  50.    HBITMAP hOldBitmap;
  51.    BITMAP bm;
  52.    HLSTR hlstr;
  53.    LPSTR RESULT;
  54.  
  55.    // LPSTR string replaced with VBDeref(hlstr)
  56.    if( BMP_GetSize( hImage) > 0)
  57.     { hlstr = VBCreateTempHlstr( NULL, BMP_GetSize(hImage));
  58.       RESULT = VBDerefHlstr( hlstr);
  59.     }
  60.    else
  61.     { // Error occurred -- image too big to fit in the string.
  62.       ReturnVBError( OUT_OF_STRING_SPACE, "Bitmap too big to fit in string.");
  63.     }
  64.  
  65.    // Get information about bitmap size and store in bm.
  66.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  67.  
  68.    // Select VB's picture...  GET BITMAP DATA.
  69.    hOldBitmap = (HBITMAP) SelectObject( hdc, hImage);
  70.  
  71.  
  72.    // Store the width and height in a header at beginning of string
  73.    StringBMP_SetDimensions( RESULT, bm.bmWidth, bm.bmHeight, bm.bmWidthBytes);
  74.  
  75.  
  76.    // Copy bitmap data into string.
  77.    GetBitmapBits( hImage, bm.bmWidthBytes * bm.bmHeight, RESULT+sizeof( SB));
  78.  
  79.    // DeSelect VB's picture.
  80.    SelectObject( hdc, hOldBitmap);
  81.  
  82.    // Return the temporary VB string.
  83.    return hlstr;                         
  84.  }
  85.  
  86. //    DESCRIPTION:
  87. //       Function for converting string to a bitmap.
  88. //    PARAMETERS:
  89. //     string : VB string. Must contain valid bitmap info created using
  90. //          BmpToString.
  91. //       hdcSrc : VB Source Picture.hdc
  92. //     hbmpSrc : VB Source Picture.image
  93. //    RETURN VALUE:
  94. //       ON SUCCESS: Number of pixels in the image.
  95. //       ON FAILURE: Return VB runtime error.
  96. //    SYNTAX IN VB:
  97. //      ret& = StringToBmp( A$, Picture1.hdc, Picture1.image)
  98. DLL(LONG) StringToBMP( LPSTR A, HDC hdcSrc, HBITMAP hbmpSrc)
  99.  { // Local variables.
  100.    HBITMAP hOldBitmapNew;
  101.    HBITMAP hOldBitmapSrc;
  102.    HBITMAP hNewBitmap;
  103.    HDC hdcNew;
  104.    WORD wA, hA, wbA;
  105.  
  106.    // Make sure parameter is bitmap string.
  107.    StringBMP_ValidateParameters( A,NULL);
  108.    
  109.    // Get width and height from first chars of string.
  110.    wA = StringBMP_GetWidth(A);
  111.    hA = StringBMP_GetHeight(A);
  112.    wbA = StringBMP_GetWidthBytes(A);
  113.       
  114.    // Create a new bitmap.
  115.    hdcNew = CreateCompatibleDC( hdcSrc);
  116.    hOldBitmapSrc = (HBITMAP) SelectObject( hdcSrc, hbmpSrc);
  117.    hNewBitmap = CreateCompatibleBitmap( hdcSrc, wA, hA);
  118.  
  119.    // Select the new bitmap.
  120.    hOldBitmapNew = (HBITMAP) SelectObject( hdcNew, hNewBitmap);
  121.  
  122.    // Copy bitmap data into string.
  123.    SetBitmapBits( hNewBitmap, wbA*hA, A+sizeof( SB));
  124.  
  125.    // Copy the new bitmap to VB's
  126.    BitBlt( hdcSrc, 0,0,wA,hA, hdcNew, 0,0, SRCCOPY);
  127.  
  128.    // DeSelect bitmaps.
  129.    SelectObject( hdcSrc, hOldBitmapSrc);
  130.    SelectObject( hdcNew, hOldBitmapNew);
  131.  
  132.    // Delete the new bitmap.
  133.    DeleteObject( hNewBitmap);
  134.    DeleteDC( hdcNew);
  135.  
  136.    return (wbA*hA);
  137.  }
  138.  
  139. //    DESCRIPTION:
  140. //        Return required size in bytes for a string to hold a bitmap.
  141. //    PARAMETERS:
  142. //      hImage : VB Picture.image
  143. //    RETURN VALUE:
  144. //        ON SUCCESS: Number of bytes to initialize a string size.
  145. //        ON FAILURE: Runtime error returned to VB.
  146. //    SYNTAX IN VB:
  147. //      bytes& = Bmp_GetSize( Picture1.Image)
  148. //      if bytes = 0 then stop ' Error! Picture too big to fit in string.
  149. //
  150. DLL(LONG)  BMP_GetSize( HBITMAP hImage)
  151.  { // Local variables.
  152.    DWORD bwB, hB;
  153.  
  154.    // Get information about bitmap size.
  155.    BITMAP bm;
  156.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  157.  
  158.    bwB = bm.bmWidthBytes;
  159.    hB = bm.bmHeight;
  160.  
  161.  
  162.    // Check for error condition
  163.    if( (bwB * hB + sizeof( SB) )>MAX_VB_STRING_LENGTH)
  164.     { return 0;
  165.     }
  166.  
  167.    // Calculate size of string
  168.    return( sizeof( SB) + hB * bwB);
  169.  
  170.  }
  171.  
  172. //     DESCRIPTION:
  173. //        Binary COMPARE two bitmap strings.
  174. //    PARAMETERS:
  175. //        A, B: String to be compared.
  176. //     RETURN VALUE:
  177. //           On success: Return the number of pixels difference.
  178. //           On error: VB runtime error.
  179. //     SYNTAX IN VB:
  180. //        bytes& = StringBMP_COMPARE( A$, B$)
  181. //
  182. DLL(LONG) StringBMP_Compare( LPSTR A, LPSTR B)
  183.  { // Local variables.
  184.    WORD wA, hA, wbA;
  185.    WORD h,w;
  186.    DWORD DifferenceCount;
  187.    LPSTR ptrA, ptrB, ptrLINEA, ptrLINEB;
  188.  
  189.    // Validate parameters.
  190.    StringBMP_ValidateParameters( A,B);
  191.    
  192.    // Get bitmap dimensions.
  193.    wA = StringBMP_GetWidth( A);
  194.    hA = StringBMP_GetHeight( A);
  195.    wbA = StringBMP_GetWidthBytes( A);
  196.  
  197.  
  198.    // Set Pointers to beginning of data in string.
  199.    ptrLINEA = A + sizeof( SB);
  200.    ptrLINEB = B + sizeof( SB);
  201.  
  202.    // Compare the strings.
  203.    DifferenceCount = 0L; 
  204.    for( h=0; h<hA; h++)
  205.     { ptrA = ptrLINEA;
  206.       ptrB = ptrLINEB;
  207.       ptrLINEA += wbA;
  208.       ptrLINEB += wbA;
  209.       for( w=0; w<wA; w++)
  210.        { if (*(ptrA++) !=  *(ptrB++))
  211.           { DifferenceCount++;   // Count # of deltas.
  212.           }
  213.        }
  214.     }
  215.  
  216.    return DifferenceCount;
  217.  }
  218.  
  219.  
  220. //     DESCRIPTION:
  221. //        Binary OR two bitmap strings.
  222. //    PARAMETERS:
  223. //        A, B: VB strings to be or'ed.
  224. //     RETURN VALUE:
  225. //           On success: Return the or'ed string.
  226. //           On error: VB runtime error.
  227. //     SYNTAX IN VB:
  228. //        C$ = StringBMP_OR( A$, B$)
  229. //
  230. DLL(HLSTR)  StringBMP_OR( LPSTR A, LPSTR B)
  231.  { // Local variables.
  232.    WORD wbA, hA, wA;
  233.    WORD h, w;
  234.    LPSTR ptrA, ptrB, ptrRESULT;
  235.    LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
  236.    LPSTR RESULT;
  237.    HLSTR hlstr;
  238.  
  239.    // Validate parameters.
  240.    StringBMP_ValidateParameters( A,B);
  241.       
  242.    // Get bitmap dimensions.
  243.    wA = StringBMP_GetWidth( A);
  244.    hA = StringBMP_GetHeight( A);
  245.    wbA = StringBMP_GetWidthBytes( A);
  246.  
  247.    // Create temporary VB string. RESULT = pointer to string.
  248.    hlstr = VBCreateTempHlstr( NULL, sizeof( SB) + hA * wbA);
  249.    RESULT = VBDerefHlstr( hlstr);
  250.  
  251.    // Set dimensions of output string. 
  252.    StringBMP_SetDimensions( RESULT, wA, hA, wbA);
  253.  
  254.    // Set Pointers to beginning of data in string.
  255.    ptrLINEA = A + sizeof( SB);
  256.    ptrLINEB = B + sizeof( SB);
  257.    ptrLINERESULT = RESULT + sizeof( SB);
  258.  
  259.    // OR the strings.
  260.    for( h=0; h<hA; h++)
  261.     { ptrA = ptrLINEA;
  262.       ptrB = ptrLINEB;
  263.       ptrRESULT = ptrLINERESULT;
  264.       ptrLINEA += wbA;
  265.       ptrLINEB += wbA;
  266.       ptrLINERESULT += wbA;
  267.       for( w=0; w<wA; w++)
  268.        { *(ptrRESULT++) = (*(ptrA++) & *(ptrB++));
  269.        }
  270.  
  271.     }
  272.    return hlstr;
  273. }
  274.  
  275. //     DESCRIPTION:
  276. //        Binary AND two bitmap strings.
  277. //    PARAMETERS:
  278. //        A, B: VB strings to be and'ed.
  279. //     RETURN VALUE:
  280. //           On success: Return the and'ed string.
  281. //           On error: VB runtime error.
  282. //     SYNTAX IN VB:
  283. //        C$ = StringBMP_AND( A$, B$)
  284. //
  285. DLL(HLSTR)  StringBMP_AND( LPSTR A, LPSTR B)
  286.  { // Local variables.
  287.    WORD wbA, hA, wA;
  288.    WORD h, w;
  289.    LPSTR ptrA, ptrB, ptrRESULT;
  290.    LPSTR ptrLINEA, ptrLINEB, ptrLINERESULT;
  291.    LPSTR RESULT;
  292.    HLSTR hlstr;
  293.  
  294.    // Validate parameters.
  295.    StringBMP_ValidateParameters( A,B);
  296.  
  297.    // Get bitmap dimensions.
  298.    wA = StringBMP_GetWidth( A);
  299.    hA = StringBMP_GetHeight( A);
  300.    wbA = StringBMP_GetWidthBytes( A);
  301.  
  302.    // Create temporary VB string. RESULT = pointer to string.
  303.    hlstr = VBCreateTempHlstr( NULL, hA*wbA+sizeof(SB));
  304.    RESULT = VBDerefHlstr( hlstr);
  305.  
  306.    // Set dimensions of output string.
  307.    StringBMP_SetDimensions( RESULT, wA, hA, wbA);
  308.  
  309.    // Set Pointers to beginning of data in string.
  310.    ptrLINEA = A + sizeof( SB);
  311.    ptrLINEB = B + sizeof( SB);
  312.    ptrLINERESULT = RESULT + sizeof( SB);
  313.  
  314.    // AND the strings.
  315.    for( h=0; h<hA; h++)
  316.     { ptrA = ptrLINEA;
  317.       ptrLINEA += wbA;
  318.  
  319.       ptrB = ptrLINEB;
  320.       ptrLINEB += wbA;
  321.  
  322.       ptrRESULT = ptrLINERESULT;
  323.       ptrLINERESULT += wbA;
  324.  
  325.       for( w=0; w<wA; w++)
  326.        { *(ptrRESULT++) = (*(ptrA++) | *(ptrB++));
  327.        }
  328.     }
  329.  
  330.    // Return VB string.
  331.    return hlstr;
  332.  }
  333.  
  334. //     DESCRIPTION:
  335. //        Binary INVERT a bitmap string.
  336. //    PARAMETERS:
  337. //        A: VB string to be inverted.
  338. //     RETURN VALUE:
  339. //           On success: Return the inverted string.
  340. //           On error: VB runtime error.
  341. //     SYNTAX IN VB:
  342. //        A$ = StringBMP_INVERT( B$)
  343. //
  344. DLL(HLSTR)  StringBMP_INVERT( LPSTR A)
  345.  { // Local variables.
  346.    LONG i, size;
  347.    LPSTR ptrA, ptrRESULT;
  348.    LPSTR RESULT;
  349.    HLSTR hlstr;
  350.  
  351.    // Validate parameters.
  352.    StringBMP_ValidateParameters( A,NULL); 
  353.    
  354.    // Calculate size in bytes.
  355.    size = StringBMP_GetWidthBytes(A)*StringBMP_GetHeight(A); 
  356.  
  357.    // Create temporary VB string. RESULT = pointer to string.
  358.    hlstr = VBCreateTempHlstr( NULL, size+sizeof(SB));
  359.    RESULT = VBDerefHlstr( hlstr);
  360.  
  361.    // Set Pointers to beginning of data in string.
  362.    ptrA = A + sizeof( SB);
  363.    ptrRESULT = RESULT + sizeof( SB);
  364.  
  365.    // Invert the string.
  366.    for( i=0; i<size; i++)
  367.     { *(ptrRESULT++) = ~*(ptrA++);
  368.     }
  369.  
  370.    return (hlstr);
  371.  }
  372.  
  373. //     DESCRIPTION:
  374. //        Fills a bitmap with same color pixels.
  375. //    PARAMETERS:
  376. //        A: VB string to be filled with bitmap.
  377. //        CHAR: VB string containing fill character. Only leftmost
  378. //            character is used.
  379. //     RETURN VALUE:
  380. //           On success: Return the filled string.
  381. //           On error: VB runtime error.
  382. //     SYNTAX IN VB:
  383. //        A$ = StringBMP_Fill( B$, chr$(0))
  384. //
  385. DLL(HLSTR) StringBMP_Fill( LPSTR A, LPSTR CHAR)
  386.  { // Local variables.
  387.    LONG i, size;
  388.    LPSTR ptrRESULT;
  389.    LPSTR RESULT;
  390.    HLSTR hlstr;
  391.  
  392.    // Validate parameters.
  393.    StringBMP_ValidateParameters( A, NULL); 
  394.  
  395.    // Calculate size in bytes.
  396.    size = StringBMP_GetWidthBytes(A)*StringBMP_GetHeight(A); 
  397.  
  398.    // Create temporary VB string. RESULT = pointer to string.
  399.    hlstr = VBCreateTempHlstr( NULL, size+sizeof(SB));
  400.    RESULT = VBDerefHlstr( hlstr);
  401.  
  402.    // Set Pointers to beginning of data in string.
  403.    ptrRESULT = RESULT + sizeof( SB);
  404.  
  405.    // Fill the string with the leftmost character in A.
  406.    for( i=0; i<size; i++)
  407.     { *(ptrRESULT++)=CHAR[0];
  408.     }
  409.  
  410.    // Return the filled string.
  411.    return hlstr;
  412.  }
  413.  
  414. //    DESCRIPTION:
  415. //        Returns the height in pixels of a bitmap.
  416. //    PARAMETERS:
  417. //      hImage : VB Picture.image
  418. //    RETURN VALUE:
  419. //      Height in pixels.
  420. //    SYNTAX IN VB:
  421. //      Should not be called from VB.
  422. //
  423. DLL(LONG)  BMP_GetHeight( HBITMAP hImage)
  424.  { // Get information about bitmap size.
  425.    BITMAP bm;
  426.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  427.    return ( bm.bmHeight);
  428.  }
  429.  
  430. //    DESCRIPTION:
  431. //        Returns the width in pixels of a bitmap.
  432. //    PARAMETERS:
  433. //      hImage : VB Picture.image
  434. //    RETURN VALUE:
  435. //      Width in pixels.
  436. //    SYNTAX IN VB:
  437. //      Should not be called from VB.
  438. //
  439. DLL(LONG)  BMP_GetWidth( HBITMAP hImage)
  440.  { // Get information about bitmap size.
  441.    BITMAP bm;
  442.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  443.    return( bm.bmWidthBytes);
  444.  }
  445.  
  446.  
  447. //    DESCRIPTION:
  448. //        Store the dimensions of the bitmap in a header at the beginning
  449. //              of the string.
  450. //    PARAMETERS:
  451. //           A : VB string
  452. //        w : width of VB bitmap to be stored in the string.
  453. //        h : height of VB bitmap to be stored in the string.
  454. //        wb : width in bytes (always even) of VB bitmap to be stored.
  455. //    RETURN VALUE:
  456. //        ON SUCCESS: 1
  457. //    SYNTAX IN VB:
  458. //      Should not be called from VB.
  459. //
  460. DLL(LONG) StringBMP_SetDimensions( LPSTR A, int w, int h, int wb)
  461.  { union STRINGBMP temp;
  462.    temp.vbstring = A;
  463.    temp.stringbmp->identifier = StringBmpIdentifier;
  464.    temp.stringbmp->width = w;
  465.    temp.stringbmp->height = h;
  466.    temp.stringbmp->widthbytes = wb;
  467.    return 1;
  468.  }
  469.  
  470. //    DESCRIPTION:
  471. //        Examine the header of a string containing bitmap info and
  472. //        determine the bitmap width.
  473. //    PARAMETERS:
  474. //      A : VB string containing bitmap info.
  475. //    RETURN VALUE:
  476. //        width in pixels of the stored bitmap.
  477. //    SYNTAX IN VB:
  478. //      Should not be called from VB.
  479. //
  480. DLL(WORD) StringBMP_GetWidth( LPSTR A)
  481.  { union STRINGBMP temp;
  482.    //StringBMP_ValidateParameters( A,NULL);
  483.    temp.vbstring = A;
  484.    return temp.stringbmp->width;
  485.  }
  486.  
  487. //    DESCRIPTION:
  488. //        Examine the header of a string containing bitmap info and
  489. //        determine the bitmap height.
  490. //    PARAMETERS:
  491. //      A : VB string containing bitmap info.
  492. //    RETURN VALUE:
  493. //        Height in pixels of the stored bitmap.
  494. //    SYNTAX IN VB:
  495. //      Should not be called from VB.
  496. //
  497. DLL(WORD) StringBMP_GetHeight( LPSTR A)
  498.  { union STRINGBMP temp;
  499.    //StringBMP_ValidateParameters( A,NULL);
  500.    temp.vbstring = A;
  501.    return temp.stringbmp->height;
  502.  }
  503.  
  504. //    DESCRIPTION:
  505. //        Examine the header of a string containing bitmap info and
  506. //        determine the bitmap width in bytes. (Always even).
  507. //    PARAMETERS:
  508. //      A : VB string containing bitmap info.
  509. //    RETURN VALUE:
  510. //        Width in bytes of the stored bitmap.
  511. //    SYNTAX IN VB:
  512. //      Should not be called from VB.
  513. //
  514. DLL(WORD) StringBMP_GetWidthBytes( LPSTR A)
  515.  { union STRINGBMP temp;
  516.    //StringBMP_ValidateParameters( A,NULL);
  517.    temp.vbstring = A;
  518.    return temp.stringbmp->widthbytes;
  519.   }
  520.  
  521. //    DESCRIPTION:
  522. //        Examine the header of a string containing bitmap info and
  523. //        return the "identifier word". This word is used to check
  524. //        whether the string contains valid bitmap info.
  525. //    PARAMETERS:
  526. //           A : VB string containing bitmap info.
  527. //    RETURN VALUE:
  528. //        Identifier word. Should be 'SB' (0x5342)
  529. //    SYNTAX IN VB:
  530. //      Should not be called from VB.
  531. DLL( WORD) StringBmp_VerifyIdentifier( LPSTR A)
  532.  { union STRINGBMP temp;
  533.    temp.vbstring = A;
  534.    if( temp.stringbmp->identifier != StringBmpIdentifier)
  535.     { ReturnVBError(ILLEGAL_FUNCTION_CALL,"String does not contain a valid bitmap.");
  536.     }
  537.    return temp.stringbmp->identifier;
  538.  }
  539.  
  540. //    DESCRIPTION:
  541. //        Examine the header of two strings containing bitmap info and
  542. //        make sure they are the same size.
  543. //    PARAMETERS:
  544. //      A : VB string containing bitmap info.
  545. //      B : VB string containing bitmap info.
  546. //    RETURN VALUE:
  547. //        ON SUCCESS: 0 = bitmaps are the same size.
  548. //        ON FAILURE: Return VB runtime error.
  549. //    SYNTAX IN VB:
  550. //      Should not be called from VB.
  551. DLL(WORD)  StringBMP_ValidateParameters( LPSTR A, LPSTR B)
  552.  { WORD wA, hA, wbA;
  553.    WORD wB, hB, wbB;
  554.  
  555.    // Check bitmap A:
  556.    if (A != NULL)
  557.     { StringBmp_VerifyIdentifier( A);
  558.       wA = StringBMP_GetWidth( A);
  559.       hA = StringBMP_GetHeight( A);
  560.       wbA = StringBMP_GetWidthBytes( A);
  561.     }
  562.  
  563.    // Get size of bitmap B.
  564.    if( B != NULL)
  565.     { StringBmp_VerifyIdentifier( B);
  566.       wB = StringBMP_GetWidth( B);
  567.       hB = StringBMP_GetHeight( B);
  568.       wbB = StringBMP_GetWidthBytes( B);
  569.     }
  570.  
  571.    // Make sure all bitmaps the same size.
  572.    if( (A != NULL) && (B !=NULL) )
  573.     { if ( !( (wbA == wbB) && (hA == hB) && (wA == wB) ) )
  574.        { ReturnVBError(ILLEGAL_FUNCTION_CALL,"Function requires two same-size strings.");
  575.        }
  576.     }
  577.    // Everything OK.
  578.    return 0;
  579.  }
  580.  
  581.  
  582.  
  583.